package com.gemserk.infraestructure.lwjglmavenizer;

import java.io.File;

public class MavenDeployer {


	public static File makeRelativeTo(File file, File relativeTo){
		if(relativeTo == null)
			return file
		
		String fileAbsolutePath = file.getAbsolutePath()
		String relativeToAbsolutePath = relativeTo.getAbsolutePath()
		if(!fileAbsolutePath.startsWith(relativeToAbsolutePath))
			return file
		
		def relativePath = fileAbsolutePath.substring(relativeToAbsolutePath.length()+1)
		return new File(relativePath)
	}
	
	static def deploy(String repoUrl, String repoId, File pom, File artifact, String classifier, File relativeTo){
		
		if(!pom.exists()){
			return "The pom doesnt exist  - $artifact - $classifier"
		}			
		if(!artifact.exists()){
			return "The artifact doesnt exist - $artifact - $classifier"
		}
		return  "mvn deploy:deploy-file -Durl=$repoUrl -DrepositoryId=$repoId -Dfile=${makeRelativeTo(artifact,relativeTo)} -DpomFile=${makeRelativeTo(pom, relativeTo)} ${classifier != null ? "-Dclassifier=$classifier" : ""}"
	}

	static def install(File pom, File artifact, String classifier, File relativeTo){
		if(!pom.exists()){
			return "The pom doesnt exist  - $artifact - $classifier"
		}			
		if(!artifact.exists()){
			return "The artifact doesnt exist  - $artifact - $classifier"
		}
		return  "mvn install:install-file -Dfile=${makeRelativeTo(artifact,relativeTo)} -DpomFile=${makeRelativeTo(pom, relativeTo)} ${classifier != null ? "-Dclassifier=$classifier" : ""}"
	}
	
}